<?php
/**
 * Created by PhpStorm.
 * User: XYX
 * Date: 2017/5/29
 * Time: 20:31
 */

namespace App\MakeTable;


use Illuminate\Support\Facades\Auth;

class Table
{
    private $tablehead;
    private $page;

    public function __construct($tablehead,$page)
    {
        $this->tablehead = $tablehead;
        $this->page = $page;

    }

    public function viewpagedata($modelname){

        $modeldata = $modelname::orderBy('created_at', 'desc')->paginate($this->page);
        return $modeldata;

    }

    public function viewdata($modelname){
        $tablehead = $this->tablehead;
        $modeldata = $this->viewpagedata($modelname);

        $modeltabledata = collect($modeldata->items())->map(function($model) use ($tablehead){

            return $model->keytodata($tablehead);
        })->toArray();
       return $modeltabledata;
    }
    public function viewpagedatawithuser($modelname){

        $modeldata = $modelname::orderBy('created_at', 'desc')->where('club_id','=',Auth::user()->clubroleid())->paginate($this->page);

        return $modeldata;

    }

    public function viewdatawithuser($modelname){
        $tablehead = $this->tablehead;
        $modeldata = $this->viewpagedatawithuser($modelname);
        $modeltabledata = collect($modeldata->items())->map(function($model) use ($tablehead){

            return $model->keytodata($tablehead);
        })->toArray();

        return $modeltabledata;
    }
}